import { uploadFiles } from '../../api/file'
import { GlobalData } from '../../store/state'

Page({
  data: {
    isFullScreen: GlobalData.isFullScreen,
    formats: {},
    readOnly: false,
    placeholder: '开始输入...',
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false,
    content: '',
    id: 0,
    show: false,
  },
  readOnlyChange() {
    this.setData({
      readOnly: !this.data.readOnly,
    })
  },
  onLoad(query) {
    this.setData({
      isFullScreen: GlobalData.isFullScreen,
    })
    const pages = getCurrentPages()
    const lastpage = pages[pages.length - 2]
    if (query.type === 'knowledge' && +query.id === 1) {
      wx.setNavigationBarTitle({
        title: '编辑免费内容',
      })
      this.setData({
        placeholder: '编辑免费内容',
        content: lastpage.data.free_content,
        id: 1,
      })
    } else if (query.type === 'knowledge' && +query.id === 2) {
      wx.setNavigationBarTitle({
        title: '编辑内容',
      })
      this.setData({
        placeholder: '编辑内容',
        content: lastpage.data.pay_content,
        id: 2,
      })
    } else {
      wx.setNavigationBarTitle({
        title: query.type === 'wendas' ? '编辑队伍描述' : '编辑活动详情',
      })
      this.setData({
        placeholder: query.type === 'wendas' ? '编辑队伍描述' : '编辑活动详情',
        content: lastpage.data.detail,
      })
    }
    const platform = wx.getSystemInfoSync().platform
    const isIOS = platform === 'ios'
    this.setData({ isIOS })
    const that = this
    this.updatePosition(0)
    let keyboardHeight = 0
    wx.onKeyboardHeightChange((res) => {
      if (res.height === keyboardHeight) return
      const duration = res.height > 0 ? res.duration * 1000 : 0
      keyboardHeight = res.height
      setTimeout(() => {
        wx.pageScrollTo({
          scrollTop: 0,
          success() {
            that.updatePosition(keyboardHeight)
            that.editorCtx.scrollIntoView()
          },
        })
      }, duration)
    })
  },
  updatePosition(keyboardHeight) {
    const toolbarHeight = 50
    const { windowHeight, platform } = wx.getSystemInfoSync()
    let editorHeight =
      keyboardHeight > 0 ? windowHeight - keyboardHeight - toolbarHeight : windowHeight
    this.setData({ editorHeight, keyboardHeight })
  },
  calNavigationBarAndStatusBar() {
    const systemInfo = wx.getSystemInfoSync()
    const { statusBarHeight, platform } = systemInfo
    const isIOS = platform === 'ios'
    const navigationBarHeight = isIOS ? 44 : 48
    return statusBarHeight + navigationBarHeight
  },
  onEditorReady() {
    const that = this
    wx.createSelectorQuery()
      .select('#editor')
      .context(function(res) {
        that.editorCtx = res.context
        that.editorCtx.setContents({
          html: that.data.content, //这里就是获取上一页面中的数据
          success: (res) => {
            console.log(res)
          },
          fail: (res) => {
            console.log(res)
          },
        })
      })
      .exec()
  },
  blur() {
    this.editorCtx.blur()
  },
  format(e) {
    let { name, value } = e.target.dataset
    if (!name) return
    this.editorCtx.format(name, value)
  },
  onStatusChange(e) {
    const formats = e.detail
    this.setData({ formats })
  },
  insertDivider() {
    this.editorCtx.insertDivider({
      success: function() {
        console.log('insert divider success')
      },
    })
  },
  clear() {
    this.editorCtx.clear({
      success: function(res) {
        console.log('clear success')
      },
    })
  },
  removeFormat() {
    this.editorCtx.removeFormat()
  },
  insertDate() {
    const date = new Date()
    const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    this.editorCtx.insertText({
      text: formatDate,
    })
  },
  insertImage() {
    const that = this
    wx.chooseImage({
      count: 1,
      success: (res) => {
        this.setData({
          show: true,
        })
        uploadFiles(res.tempFilePaths[0])
          .then((ress) => {
            that.editorCtx.insertImage({
              src: ress.uri,
              width: '100%',
              success: () => {
                console.log('insert image success')
                this.setData({ show: false })
              },
            })
          })
          .catch(() => {
            this.setData({ show: false })
          })
      },
    })
  },
  /** 加载中 */
  show() {
    wx.showLoading({
      title: '图片上传中',
    })
  },
  // 获取内容
  clickLogText(e) {
    this.editorCtx.getContents({
      success: (res) => {
        const pages = getCurrentPages()
        const lastpage = pages[pages.length - 2]
        if (+this.data.id === 1) {
          lastpage.setData({
            free_content: res.html,
          })
        } else if (+this.data.id === 2) {
          lastpage.setData({
            pay_content: res.html,
          })
        } else {
          // 活动
          lastpage.setData({
            detail: res.html,
          })
        }
        wx.navigateBack({
          delta: 1,
        })
      },
    })
  },
})
